﻿using System;
using System.IO;
using Presentation;

namespace Infrastructure
{
    public class TxtLogger : ILogger
    {
        public string Name { get; private set; }
        public bool CanWriteTimeMarks { get; private set; }

        public TxtLogger(bool canWriteTimeMarks = true)
        {
            CanWriteTimeMarks = canWriteTimeMarks;
            var creationTime = DateTime.Now.ToString("yyyy-MMM-dd_HH-mm-ss");
            Name = creationTime + "_" + "log.txt";
            var writer = new StreamWriter(Name, false);
            writer.Close();
        }
        public void Write(string text, bool writeTimeMark = true)
        {
            var time = DateTime.Now.ToString("HH:mm:ss");
            using (var writer = new StreamWriter(Name, true))
            {
                var line = "";
                if (CanWriteTimeMarks)
                {
                    if (writeTimeMark)
                    {
                        line = "[" + time + "] ";
                    }
                    else
                    {
                        line = "           ";
                    }
                }
                line += text;
                writer.WriteLine(line);
                writer.Close();
            }
        }
        public string Read()
        {
            var text = "";
            using (var reader = new StreamReader(Name))
            {
                while (!reader.EndOfStream)
                {
                    text += reader.ReadLine() + @"\n";
                }
                reader.Close();
            }
            return text;
        }
    }
}
